package com.company.cuobuliao.main;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.util.AbToastUtil;
import com.company.cuobuliao.R;
import com.company.cuobuliao.fragment.Fragment1;
import com.company.cuobuliao.fragment.Fragment2;
import com.company.cuobuliao.ui.scan.CustomScanActivity;
import com.company.cuobuliao.zxingutil.CaptureActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends AppCompatActivity {

    private Context context;
    private long time = 0;
    private Fragment1 f1;
    private Fragment2 f2;
    private static LinearLayout[] mTabs;
    private static TextView[] mTabsText;
    private static Fragment[] fragments;
    private static int index;
    // 当前fragment的index
    private static int currentTabIndex;

    private static final int REQUEST_CODE_SCAN = 0x0000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        // 初始化界面
        initView();
        initBottomView();
    }

    private void initView() {
        f1 = new Fragment1();
        f2 = new Fragment2();
        fragments = new Fragment[]{f1, f2};
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_content, f1)
                .commit();

        findViewById(R.id.ivScan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                customScan();
                Intent intent = new Intent(MainActivity.this,
                        CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
            }
        });
    }

    private void initBottomView() {
        mTabs = new LinearLayout[2];
        mTabs[0] = (LinearLayout) findViewById(R.id.lilyHome);
        mTabs[1] = (LinearLayout) findViewById(R.id.lilyKu);
        mTabsText = new TextView[2];
        mTabsText[0] = (TextView) findViewById(R.id.tvHome);
        mTabsText[1] = (TextView) findViewById(R.id.tvKu);
        // 把第一个tab设为选中状态
        currentTabIndex = 0;
    }


    /**
     * button点击事件
     *
     * @param view
     */
    public void onTabClicked(View view) {
        ((ImageView) findViewById(R.id.ivHome)).setImageResource(R.drawable.bg_select_home);
        switch (view.getId()) {
            case R.id.lilyHome:
                index = 0;
                break;
            case R.id.lilyKu:
                index = 1;
                break;
        }
        setTab(index);
        if (currentTabIndex != index) {
            FragmentTransaction trx = ((FragmentActivity) context)
                    .getSupportFragmentManager().beginTransaction();
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_content, fragments[index]);
            }
            trx.hide(fragments[currentTabIndex]);
            trx.show(fragments[index]).commit();
        }
        currentTabIndex = index;
    }

    public void setTab(int index) {
        mTabs[0].setSelected(false);
        mTabs[1].setSelected(false);
        mTabs[index].setSelected(true);
        for (int i = 0; i < mTabsText.length; i++) {
            if (i == index) {
                mTabsText[i].setTextColor(getResources().getColor(
                        R.color.orange));
            } else {
                mTabsText[i].setTextColor(getResources().getColor(
                        R.color.gray_text));
            }
        }
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - time > 2000) {
            AbToastUtil.showToast(context, "再按一次退出");
            time = currentTime;
        } else {
            super.onBackPressed();
        }
    }

    // 你也可以使用简单的扫描功能，但是一般扫描的样式和行为都是可以自定义的，这里就写关于自定义的代码了
    // 你可以把这个方法作为一个点击事件
    public void customScan() {
        new IntentIntegrator(this)
                .setOrientationLocked(false)
                .setCaptureActivity(CustomScanActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }

    @Override
    // 通过 onActivityResult的方法获取 扫描回来的 值
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(this, "内容为空", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "扫描成功", Toast.LENGTH_LONG).show();
                // ScanResult 为 获取到的字符串
                String ScanResult = intentResult.getContents();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
